/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package syncmellariusnote;

import com.sqlite.SQLiteJDBC;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import syncmellariusnote.helpers.AccessObjectRepo;
import syncmellariusnote.helpers.ConnectorConfigurator;
import syncmellariusnote.helpers.FileSystemHelper;
import syncmellariusnote.helpers.RepoHelper;

/**
 *
 * @author Vitor
 */

public class Sequencer implements SyncInterface {
    File shema = null;
    File template = null;
    Receptor r = new Receptor();
    Sender s = new Sender();
    RepoHelper repoHelper = new RepoHelper();  
    public Sequencer (){
        File f = new File(System.getProperty("java.class.path"));
        File dir = f.getAbsoluteFile().getParentFile();
        String path = dir.getPath();
         shema= new File(path+"/dbSchema/MellariosShemaDb.db3");
         template= new File(path+"/template/mellariusBd.db3");
       //shema= new File("../SyncMellariusNote/dbSchema/MellariosShemaDb.db3");
       //template= new File("../SyncMellariusNote/template/mellariusBd.db3");
         
    }
    @Override
    public void retriveDataBase() {
        try {
            
            FileSystemHelper.copyFileToDirectory(shema, template);  
            AccessObjectRepo accessObjectRepo = repoHelper.populateRepoAccess();
            SQLiteJDBC sqliteJdbc = new SQLiteJDBC(template.toString());
            sqliteJdbc.fillSqliteDataBase(accessObjectRepo);  
            s.sendFile(ConnectorConfigurator.getClientConnection(),template.toString());
            sqliteJdbc.closeConnection();
            FileSystemHelper.deleteFile(template);
            
        } catch (IOException | SQLException | ClassNotFoundException ex) {
            Logger.getLogger(Sequencer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void bdActualization() {
        try {
        
            r.receiveFile(template.toString());
            SQLiteJDBC sqliteJdbc = new SQLiteJDBC(template.toString());
            sqliteJdbc.fillAccessObjects();
            AccessObjectRepo accessObjectRepo = repoHelper.populateRepoAccess();
            sqliteJdbc.closeConnection();
            retriveDataBase();
        } catch (SQLException | ClassNotFoundException | IOException ex) {
            Logger.getLogger(Sequencer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
}
